<?php
namespace Home\Controller;
class LoginController extends CommonController{
	//用户登录
	public function index(){
		$this->add_step('登录');
		$jumpurl = session('login_jumpurl');
		if(!$jumpurl){
			session('login_jumpurl', $_SERVER['HTTP_REFERER']);
			$jumpurl = session('login_jumpurl');
		}
		$login_error_num = session('login_error_num');
		$is_show_imgcod  = $login_error_num >= 3;
		$this->assign('is_show_imgcod', $is_show_imgcod);
		if(IS_POST){
			$mobile    = $this->_post('mobile', '手机号不能为空！');
			$password  = $this->_post('password', '密码不能为空！');
			$password  = md5($password);
			if($is_show_imgcod){
				$imgcode = $this->_post('imgcode', '图片验证码不能为空！');
				$verify  = new \Think\Verify();
    			if(!$verify->check($imgcode)){
    				$this->error('您输入验证码不正确！');
    			}
			}
			$user_info = D('User')->login($mobile, $password);
			if($user_info){
				session('login_jumpurl', null);
				D('User')->save_session($user_info);
				session('login_error_num', 0);
				$jumpurl = $jumpurl ? $jumpurl : U('Index/index');
				// 律师信息未完善处理
				if(!$user_info['info_is_full'] && $user_info['is_lawyer'] == 1){
					$jumpurl = U('LawyerCenter/perfect_data');
				}
				$this->success('登录成功！', $jumpurl);
			}else{
				$login_error_num = $login_error_num + 1;
				session('login_error_num', $login_error_num);
				$this->error(D('User')->tips_info, '', array('is_show_imgcod' => $is_show_imgcod));
			}
		}
		$this->display();
	}

	//微博登录
	public function sso_weibo(){
		$code = $this->_get('code');
		// 获取要跳转地址
		$jumpurl = session('login_jumpurl');
		if(!$jumpurl){
			session('login_jumpurl', $_SERVER['HTTP_REFERER']);
		}
		$jumpurl = $jumpurl ? $jumpurl : U('/');

		// 获取微博配置信息
		$weibo_config = $this->api->config('', 'weibo');
		$weibo_akey = $weibo_config['appkey'];
		$weibo_skey = $weibo_config['appsecret'];
		if(!$weibo_akey || !$weibo_skey){
			$this->error('尚未配置新浪微博登录相关参数！');
		}
		// 加载微博SDK并初始化
		load('saetv2', 'Public/Class/Weibo', '.ex.class.php');
		$oauth = new \SaeTOAuthV2( $weibo_akey , $weibo_skey );
		// 设置回调地址
		$callback_url = U('Login/sso_weibo');
		
		// 跳转到微博获取CODE
		if(!$code){
			$code_url = $oauth->getAuthorizeURL( $callback_url );
			redirect($code_url);
		}

		// 通过CODE获取用户信息
		$keys = array();
		$keys['code']         = $code;
		$keys['redirect_uri'] = $callback_url;
		try {
			$token      = $oauth->getAccessToken( 'code', $keys );
			$client     = new \SaeTClientV2( $weibo_akey , $weibo_skey , $token['access_token'] );
			
			// 用户唯一标识
			$open_id    = $token['uid'];
			//未登录用户有绑定处理
			if($this->api->sso_login('weibo', $open_id) && !$this->user_id){
				redirect($jumpurl);
			}
			//根据ID获取用户等基本信息
			$weibo_user = $client->show_user_by_id($open_id);
			// 性别
			$gender     = $weibo_user['gender'];
			$sex_gender = array('m'=>1,'f'=>2,'n'=>0);
			$sex        = $sex_gender[$gender];
			// 用户头像
			$head_url   = $weibo_user['profile_image_url'];
			// 用户昵称
			$nickname   = $weibo_user['screen_name'];
			
			//登录用户的直接绑定
			if($this->user_id){
				if($this->api->sso_bind('weibo', $open_id, $nickname)){
					redirect($jumpurl);
				}
				else{
					$this->error($this->api->api_reinfo);
				}
			}

			//未登录用户的注册并绑定处理
			if($this->api->sso_register('weibo', $open_id, $nickname, $sex, $head_url)){
				redirect($jumpurl);
			}
			else{
				$this->error($this->api->api_reinfo);
			}
		}catch (OAuthException $e) {
			$this->error('授权失败！', U('/'));
		}
	}
	//微信登录回调
	public function weixin_callback(){
		$weixin_code = $this->_get('code');
		$user_type = $this->_get('state');
		$user_type = $user_type == 'teacher' ? 'teacher' : 'student';
		if($weixin_code){
			import("WeixinWebOAuth",'Public/Class/Weixin');
			$weixin = new WeixinWebOAuth();
			$token_openid = $weixin->get_token_openid($weixin_code);
			if(!$token_openid['token']){
				$this->error('微信登录超时，请重新登录！');
			}

			$weixin_user = $weixin->get_user_info($token_openid['token'],$token_openid['openid']);
			if(!$weixin_user['unionid']){
				$this->error('微信登录超时，请重新登录！');
			}

			$params['sso_type'] = 'weixin';
			$params['open_id']  = $weixin_user['unionid'];
			$info = $this->api->get('user/web_sso_login',$params);
			//微信登录成功操作
			if($info['status'] == '200'){
				//用户信息
				$user_info  = $info['data']['user_info'];
				//用户TOKEN
				$user_token = $info['data']['token'];
				D('User')->save_session($user_info,$user_token);
				$jumpurl = session('login_jumpurl');
				$jumpurl = $jumpurl ? $jumpurl : U('Index/index');
				redirect($jumpurl);
			}
			//未绑定操作
			if($info['status'] == '410'){
				session('weixin_user_info',$weixin_user);
				redirect(U('sso_bind_mobile',array('user_type'=>$user_type,'type'=>'weixin')));
			}
			$this->error('微信登录失败，请重新登录！');
		}
	}
	
	//微信绑定注册
	public function weixin_bind_reg(){
		$this->addStep('微信绑定');
		$weixin_user = session('weixin_user_info');
		$mobile      = session('sso_bind_mobile');
		$is_reg      = $this->_get('is_reg');
		$this->assign('is_reg',$is_reg);
		if(!$weixin_user || !$mobile){
			redirect(U('index'));
		}
		if($this->isPost()){
			//用户类型
			if($is_reg){
				$params['mobile']  = $mobile;
				$info = $this->api->get('user/get_mobile_user_type',$params);
				if($info['status'] == '200'){
					$user_type = $info['data']['user_type'];
				}else{
					$this->error($info['info']);
				}
			}else{
				$user_type = $this->_post('user_type','请选择用户类型！');
			}
			$params['mobile']    = $mobile;
			$params['smscode']   = $this->_post('smscode','验证码不能为空！');
			$params['city_id']   = $this->_post('city_id');
			$params['user_type'] = $user_type;
			$params['sso_type']  = 'weixin';
			$params['open_id']   = $weixin_user['unionid'];
			$params['nickname']  = $weixin_user['nickname'];
			$params['sex']       = $weixin_user['sex'];
			$params['head_url']  = urlencode($weixin_user['headimgurl']);
			$info = $this->api->get('user/sso_register',$params);
			if($info['status']=='200'){
				session('weixin_user_info',null);
				session('sso_bind_mobile',null);
				//用户信息
				$user_info  = $info['data']['user_info'];
				//设置用户类型
				$user_info['user_type'] = $user_type;
				//用户TOKEN
				$token = $info['data']['token'];
				D('User')->save_session($user_info,$token);
				$jumpurl = U('Index/index');
				//未注册的跳转处理
				if(!$is_reg){
					$jumpurl = $user_type == 'teacher' ? U('TeacherGuide/basic_info') : U('Index/index');
				}
				redirect($jumpurl);
			}else{
				$this->error($info['info']);
			}
		}
		$city_list = $this->api->get_city_list();
		$this->assign('city_list',$city_list);
		$this->assign('is_reg',$is_reg);
		$this->assign('bind_name','微信');
		$this->assign('bind_account',$weixin_user['nickname']);
		$this->display('sso_bind_reg');
	}
	//QQ登录
	public function sso_qq(){
		$jumpurl = session('login_jumpurl');
		if(!$jumpurl){
			session('login_jumpurl', $_SERVER['HTTP_REFERER']);
		}
		$jumpurl = $jumpurl ? $jumpurl : U('/');

		C('QQ_CONNECT', $this->api->config('', 'qq'));
		require_once("Public/Class/QQ/qqConnectAPI.php");
		$qc = new \QC();
		if($this->_get('code')){
			$call_id  = $qc->qq_callback();
			$open_id  = $qc->get_openid();

			//未登录用户有绑定处理
			if($this->api->sso_login('qq', $open_id) && !$this->user_id){
				redirect($jumpurl);
			}
			//获取QQ用户信息
			$qc       = new \QC($call_id, $open_id);
			$qq_user  = $qc->get_user_info();
			$nickname = $qq_user['nickname'];
			$email    = $qq_user['email'];
			$head_url = $qq_user['figureurl_qq_2'];

			//登录用户的直接绑定
			if($this->user_id){
				if($this->api->sso_bind('qq', $open_id, $nickname)){
					redirect($jumpurl);
				}
				else{
					$this->error($this->api->api_reinfo);
				}
			}
			//未登录用户的注册并绑定处理

			if($this->api->sso_register('qq', $open_id, $nickname, 0, $head_url)){
				redirect($jumpurl);
			}
			else{
				$this->error($this->api->api_reinfo);
			}
		}else{
			$qc->qq_login();
		}
	}
}